package com.novanok.chaincollaboration;

import android.app.Activity;
import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Marcos on 28/09/2014.
 */
public class ConectionActivity extends Activity {

    private static final String TAG = "ConectionActivity";

    private NsdHelper mNsdHelper;
    private Handler mUpdateHandler;
    private ChatConnection mConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conection_activity);

        mUpdateHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String chatLine = msg.getData().getString("msg");
                ((TextView)findViewById(R.id.txtMensaje)).append(chatLine + "\n");
            }
        };

        mConnection = new ChatConnection(mUpdateHandler);
        mNsdHelper = new NsdHelper(this);
        mNsdHelper.initializeNsd();
    }

    @Override
    protected void onPause() {
        if ( mNsdHelper != null ) {
            mNsdHelper.tearDown();
        }
        super.onPause();
    }

    /*
    @Override
    protected void onResume() {
        super.onResume();
        if ( mNsdHelper != null ) {
            mNsdHelper.registerService( mConnection.getLocalPort() );
            mNsdHelper.discoverServices();
        }
    }
*/

    @Override
    protected void onDestroy() {
        mNsdHelper.tearDown();
        mConnection.tearDown();
        super.onDestroy();
    }

    public void registrarServicio(View v) {
        this.registrar();
    }

    public void initializeResolveListener(View v){
        if (this.mNsdHelper != null)
            this.mNsdHelper.initializeResolveListener();
    }

    public void initializeDiscoveryListener(View v){
        if (this.mNsdHelper != null)
            this.mNsdHelper.initializeDiscoveryListener();
    }

    public void initializeRegistrationListener(View v){
        if (this.mNsdHelper != null)
            this.mNsdHelper.initializeRegistrationListener();
    }

    private void registrar(){
        if( mConnection.getLocalPort() > -1 ) {
            mNsdHelper.registerService( mConnection.getLocalPort() );
        } else {
            Log.d( TAG, "ServerSocket isn't bound." );
        }
    }

    public void conectar(View v){
        NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();
        if ( service != null ) {
            Log.d( MainActivity.TAG, "Connecting." );
            mConnection.connectToServer( service.getHost(),
                    service.getPort() );
        } else {
            Log.d( MainActivity.TAG, "No service to connect to!" );
        }
    }

    public void sendMessage(View v){
        mConnection.sendMessage(((EditText)findViewById(R.id.txtMsg)).getText().toString());
    }

    public void descubrir(View v){
        mNsdHelper.discoverServices();
    }
}
